//
//  SVNavigationBar.m
//  ShortVideo-ccinon
//
//  Created by LIN TONG on 2017/6/19.
//  Copyright © 2017年 LIN TONG. All rights reserved.
//

#import "SVNavigationBar.h"
#import "SVConfig.h"
#import "QRCodeUtil.h"
#import "SVAccountViewController.h"
#import <OpenShareHeader.h>
#import <Social/Social.h>
#import "UIActionSheet+Blocks.h"

@interface SVNavigationBar(){
    UILabel *titleLabel;
    UIButton *scanButton;
    UIButton *accountButton;
    UIButton *backButton;
    UIButton *shareButton;
    UIButton *doneButton;
}

@end

@implementation SVNavigationBar
- (id)init{
    if(self = [super initWithFrame:CGRectMake(0, 0, __SCREEN_WIDTH__, 20+44)]){
        self.backgroundColor = UIColorMake(30, 30, 30, 1);
        
        scanButton = [[UIButton alloc]initWithFrame:CGRectMake(S(10), 20+7, S(65), 30)];
        scanButton.imageView.contentMode = UIViewContentModeLeft;
        scanButton.titleLabel.contentMode = UIViewContentModeRight;
        scanButton.imageEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
        [scanButton setImage:[UIImage imageNamed:@"nav_scan"] forState:UIControlStateNormal];
        [scanButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        scanButton.titleLabel.font = Font(13);
        scanButton.titleLabel.textAlignment = NSTextAlignmentRight;
        [scanButton setTitle:@"扫一扫" forState:UIControlStateNormal];
        [scanButton addTarget:self action:@selector(clickToScan:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:scanButton];
        
        titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, S(150), S(30))];
        titleLabel.center = CGPointMake(__SCREEN_WIDTH__/2.0, scanButton.center.y);
        [titleLabel setTextColor:[UIColor whiteColor]];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        [titleLabel setFont:Font(16)];
        [self addSubview:titleLabel];
        
        accountButton = [[UIButton alloc]initWithFrame:CGRectMake(__SCREEN_WIDTH__-S(10)-25, 20+10, 25, 25)];
        [accountButton addTarget:self action:@selector(clickToOpenAccount:) forControlEvents:UIControlEventTouchUpInside];
        [accountButton setImage:[UIImage imageNamed:@"account_icon"] forState:UIControlStateNormal];
        [accountButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        accountButton.titleLabel.font = Font(13);
        [self addSubview:accountButton];
        
        backButton = [[UIButton alloc]initWithFrame:CGRectMake(S(5), 20+7, S(65), 30)];
        backButton.imageView.contentMode = UIViewContentModeLeft;
        backButton.imageEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
        [backButton setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];
        backButton.titleLabel.font = Font(13);
        backButton.titleLabel.contentMode = UIViewContentModeRight;
        backButton.titleLabel.textAlignment = NSTextAlignmentRight;
        [backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [backButton setTitle:@"返回" forState:UIControlStateNormal];
        backButton.hidden = YES;
        [backButton addTarget:self action:@selector(clickBack:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:backButton];
        
        shareButton = [[UIButton alloc]initWithFrame:CGRectMake(__SCREEN_WIDTH__-S(10)-29, 20+8, 29, 29)];
        [shareButton addTarget:self action:@selector(clickToShare:) forControlEvents:UIControlEventTouchUpInside];
        [shareButton setBackgroundImage:[UIImage imageNamed:@"nav_share"] forState:UIControlStateNormal];
        [shareButton setHidden:YES];
        [self addSubview:shareButton];
        
        doneButton = [[UIButton alloc]initWithFrame:CGRectMake(__SCREEN_WIDTH__-S(10)-S(60), 20+7, S(60), 30)];
        [doneButton addTarget:self action:@selector(clickDone:) forControlEvents:UIControlEventTouchUpInside];
        doneButton.titleLabel.font = Font(13);
        doneButton.titleLabel.textAlignment = NSTextAlignmentRight;
        [doneButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [doneButton setTitle:@"完成" forState:UIControlStateNormal];
        [doneButton setHidden:YES];
        [self addSubview:doneButton];
    }
    return self;
}

- (void)setTitle:(NSString *)title{
    titleLabel.text = title;
}

- (void)clickBack:(UIButton *)sender{
    if(self.backHander){
        self.backHander();
    }
}

- (void)setShowBackButton:(BOOL)showBackButton{
    [scanButton setHidden:showBackButton];
    [backButton setHidden:!showBackButton];
}

- (void)setHidenAccountButton:(BOOL)hidenAccountButton{
    [accountButton setHidden:hidenAccountButton];
}

- (void)setShowShareButton:(BOOL)showShareButton{
    [shareButton setHidden:!showShareButton];
    [accountButton setHidden:showShareButton];
}

- (void)setShowDoneButton:(BOOL)showDoneButton{
    [doneButton setHidden:!showDoneButton];
    [accountButton setHidden:showDoneButton];
}

- (void)clickToScan:(UIButton *)sender{
    if(self.qrcodeHandler){
        self.qrcodeHandler();
    }
}

- (void)clickToOpenAccount:(UIButton *)sender{
    SVAccountViewController *accountVC = [[SVAccountViewController alloc]init];
    [(UINavigationController *)self.window.rootViewController pushViewController:accountVC animated:YES];
}

- (void)clickToShare:(UIButton *)sender{
    [UIActionSheet showInView:self.superview withTitle:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@[@"分享到微信朋友圈",@"分享给微信好友",@"分享到微博",@"分享到QQ"] tapBlock:^(UIActionSheet * _Nonnull actionSheet, NSInteger buttonIndex) {
        if(buttonIndex == 0){
            if([SLComposeViewController isAvailableForServiceType:SLServiceTypeSinaWeibo]){
                SLComposeViewController *slcVC =[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeSinaWeibo];
                [slcVC setInitialText:@"分享测试"];
                [slcVC setCompletionHandler:^(SLComposeViewControllerResult result){
                    if(result == SLComposeViewControllerResultDone){
                        
                    }else{
                        
                    }
                }];
                [self.window.rootViewController presentViewController:slcVC animated:YES
                                                           completion:nil];
            }
        }else if(buttonIndex == 1){
            
        }
    }];
}

- (void)clickDone:(UIButton *)sender{
    if(self.doneHandler){
        self.doneHandler();
    }
}
@end
